package com.doudou.zuul.config;

import com.doudou.zuul.entity.AppAccess;
import com.doudou.zuul.filter.handler.DefaultLoginVerifyHandler;
import com.doudou.zuul.filter.handler.HeadHandler;
import com.doudou.zuul.filter.handler.LoginVerifyHandlerChain;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Configuration
public class LoginVerifyHandlerChainConfig {

    @Bean
    public LoginVerifyHandlerChain loginVerifyHandlerChain(AppConfig appConfig) {

        Assert.notNull(appConfig, "app config of loginVerifyHandlerChain init is null! ");
        List<AppAccess> appAccesses = appConfig.getAppAccesses();
        final LoginVerifyHandlerChain loginVerifyHandlerChain = new LoginVerifyHandlerChain(new HeadHandler());
        if (!CollectionUtils.isEmpty(appAccesses)) {
            appAccesses.stream().forEach(t ->
                    loginVerifyHandlerChain.addLastHandler(new DefaultLoginVerifyHandler(null, t))
            );
        }
        return loginVerifyHandlerChain;
    }


}
